unit cls_Demonstrativo;

interface

uses Dm,dbtables,biblio;

  type TDemonstrativo = class (TObject)
  private
    Dem_codigo    :string;
  public
    Dem_descricao :string;
    function  Gravar(P_Dem_codigo:String):Byte;
    function  Recuperar(P_Dem_codigo:String):Byte;
    function  Excluir(P_Dem_Codigo:String):Byte;
    procedure Consultar(var p_q:TQuery; p_Descricao:String);
  end;

implementation

{ TDemonstrativo }

procedure TDemonstrativo.Consultar(var p_q: TQuery; p_descricao: String);
begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('Select * from Demonstrativo where Dem_descricao>=:k_dem_descricao order by dem_descricao');
  p_q.parambyname('K_Dem_descricao').asstring:=p_Descricao;
end;

function TDemonstrativo.Excluir(P_Dem_Codigo: String): Byte;
var tq:TQuery;
begin
  tq:=Tquery.Create(dmint);
  tq.databasename:=dmint.db.databasename;
  tq.active:=False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM Demonstrativo WHERE dem_Codigo=:k_dem_Codigo');
  tq.parambyname('k_dem_Codigo').asstring:=strzero(p_dem_Codigo,2);
  tq.execSQL;
  Result:=1;
  tq.close;
  tq.Free;
end;

function TDemonstrativo.Gravar(P_Dem_codigo: String): Byte;
var tq:TQuery;
begin
  tq:=Tquery.Create(DmInt);
  tq.databasename:=dmint.Db.DatabaseName;
  tq.active:=False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM Demonstrativo where Dem_codigo=:k_Dem_Codigo');
  tq.parambyname('k_Dem_Codigo').asstring:=strzero(P_Dem_codigo,2);
  tq.open;
  if tq.eof then begin
    tq.sql.clear; tq.params.clear;
    tq.sql.add('INSERT INTO Demonstrativo VALUES(:k_Dem_Codigo,:K_Dem_Descricao)');
  end else begin
    tq.sql.clear; tq.params.clear;
    tq.sql.add('UPDATE Demonstrativo SET Dem_Descricao=:k_Dem_Descricao WHERE Dem_Codigo=:k_Dem_Codigo');
  end;
  tq.parambyname('k_Dem_Codigo').asstring:=strzero(p_dem_Codigo,2);
  tq.parambyname('K_Dem_Descricao').asstring:=Dem_descricao;
  tq.close;
  tq.execsql;
  result:=1;
  tq.Free;
end;
                                          
function TDemonstrativo.Recuperar(P_Dem_codigo: String): Byte;
var tq:TQuery;
begin
  tq:=Tquery.Create(dmint);
  tq.databasename:=DmInt.Db.DatabaseName;
  tq.active:=False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM Demonstrativo where Dem_Codigo=:k_Dem_Codigo');
  tq.Params[0].asstring:=strzero(P_Dem_codigo,2);
  tq.open;
  dem_Codigo       :=p_dem_Codigo;
  if tq.eof then begin
    Dem_Descricao:='';
    Result:=0;
  end else begin
    Dem_Descricao:=tq.fieldbyname('Dem_descricao').asstring;
    Result:=1;
  end;
  tq.Close;
  tq.Free;
end;

end.
